package com.epfresh.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.api.adapter.CommonAdapter;
import com.epfresh.api.adapter.ViewHolder;
import com.epfresh.api.entity.*;
import com.epfresh.api.entity.RequestTag;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.entity.TagPager;
import com.epfresh.api.http.HttpRequest;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.widget.list.LoadMoreListView;
import com.epfresh.api.widget.ptr.PtrDefaultHandler;
import com.epfresh.api.widget.ptr.PtrFrameLayout;
import com.epfresh.api.widget.ptr.PtrHandler;
import com.epfresh.api.widget.ptr.PtrMdFrameLayout;
import com.epfresh.bean.CouponEntity;
import com.epfresh.bean.StoreGoodsPrice;
import com.epfresh.constant.Constant;
import com.epfresh.global.BaseActivity;
import com.epfresh.global.DataManager;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @date create 17-6-7
 * @author wangzhanghuan
 * @Description
 * @version 1.0
 */
public class CartCouponActivity extends BaseActivity implements LoadMoreListView.IXListViewListener {


    private LoadMoreListView mListView;
    private ArrayList<CouponEntity> list;
    private ArrayList<CouponEntity> listBest;
    private ArrayList<CouponEntity> listSelect;
    Context mContext;
    MinusAdapter adapter;
    protected View mainView;
    protected View vException;
    protected ImageView ivSelector;
    protected ImageView ivBestSelector;
    protected TextView tvExceptionDescription;
    protected ImageView ivExceptionIcon;
    private ProgressBar pb;
    private int requestSucceedCount = 0;
    private TagPager tagPager = new TagPager();

    View tvInfo, tvHistory;
//    String data;
//    String select;
//    String best;
    String storePrice;
    boolean isSelect;
    boolean isBestSelect;
    //商店商品
    Map<String, StoreGoodsPrice> mapStorePrice;

    int maxCount = 2;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null) {
            getIntent().putExtra("data", savedInstanceState.getString("data"));
            getIntent().putExtra("best", savedInstanceState.getString("best"));
            getIntent().putExtra("select", savedInstanceState.getString("select"));
            getIntent().putExtra("storePrice", savedInstanceState.getString("storePrice"));
            getIntent().putExtra("maxCount", savedInstanceState.getInt("maxCount"));
            isSelect = savedInstanceState.getBoolean("isSelect", false);
            isBestSelect = savedInstanceState.getBoolean("isBestSelect", false);

        }

        initView();
        list = new ArrayList<CouponEntity>();
        listBest = new ArrayList<CouponEntity>();
        listSelect = new ArrayList<CouponEntity>();
        mListView.setPullLoadEnable(false); // 设置可滑动
        // mListView.setXListViewListener(this);// 设置滑动监听器
        mContext = this;
        adapter = new MinusAdapter(this, list, R.layout.item_cart_coupon);
        mListView.setAdapter(adapter);
        baseback.setVisibility(View.VISIBLE);
        initHeadView();
        initData();
//        requestOrderList(0);

    }


    @Override
    protected void onToolbarBack() {
        onBackPressed();
    }

    @Override
    public void onBackPressed() {


        finish();
    }

    @Override
    public void finish() {
        Intent intent = new Intent();
//        intent.putExtra("result", new Gson().toJson(list));
//        intent.putExtra("select", new Gson().toJson(listSelect));

        DataManager.getInstance().setCoupons(list);
        DataManager.getInstance().setCouponsSelect(listSelect);
        setResult(RESULT_OK, intent);
        super.finish();
    }

    private void initData() {

//        data = DataManager.getInstance().coupons;
//        best = DataManager.getInstance().couponsBest;
//        select = DataManager.getInstance().couponsSelect;
        storePrice = DataManager.getInstance().storePriceStr;

//        Log.d("data","data:"+data);
//        Log.d("select","select:"+select);
//        Log.d("storePrice","storePrice:"+storePrice);
        if (storePrice != null && !"".equals(storePrice)) {
            mapStorePrice = new Gson().fromJson(storePrice, new TypeToken<Map<String, StoreGoodsPrice>>() {
            }.getType());
            if (mapStorePrice == null || mapStorePrice.size() == 0) {
                super.finish();
            }
        } else {
            super.finish();
        }
//        if (data != null && !"".equals(data)) {
//            ArrayList<CouponEntity> couponEntities = new Gson().fromJson(data, new TypeToken<List<CouponEntity>>() {
//            }.getType());
//            if (couponEntities != null) {
//                list.addAll(couponEntities);
//                adapter.notifyDataSetChanged();
//            } else {
//                finish();
//            }
//        } else {
//            finish();
//        }
        List<CouponEntity> couponEntities = DataManager.getInstance().coupons;
        if (couponEntities != null) {
            list.addAll(couponEntities);
            adapter.notifyDataSetChanged();
        } else {
            super.finish();
        }
        maxCount = getIntent().getIntExtra("maxCount",list.size());


        if (maxCount == 0){

        }
        couponEntities = DataManager.getInstance().couponsSelect;
        if (couponEntities != null) {
                listSelect.addAll(couponEntities);
                adapter.notifyDataSetChanged();
            } else {
            super.finish();
            }
//        if (select != null && !"".equals(select)) {
//            ArrayList<CouponEntity> couponEntities = new Gson().fromJson(select, new TypeToken<List<CouponEntity>>() {
//            }.getType());
//            if (couponEntities != null) {
//                listSelect.addAll(couponEntities);
//                adapter.notifyDataSetChanged();
//            } else {
//                finish();
//            }
//        } else {
//            finish();
//        }
//        if (best != null && !"".equals(best)) {
//            ArrayList<CouponEntity> couponEntities = new Gson().fromJson(best, new TypeToken<List<CouponEntity>>() {
//            }.getType());
//            if (couponEntities != null) {
//                listBest.addAll(couponEntities);
//
//            }
//        }

        couponEntities = DataManager.getInstance().couponsBest;
        if (couponEntities != null) {
            listBest.addAll(couponEntities);
        }
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                CouponEntity couponEntity = list.get(i);
                boolean canUse = couponEntity.isCanUse();
                if (canUse) {
                    Boolean selected = !couponEntity.getSelected();
                    couponEntity.setSelected(selected);
                    if (listSelect != null) {
                        if (selected && !listSelect.contains(couponEntity)) {
                            listSelect.add(couponEntity);
                        } else {
                            listSelect.remove(couponEntity);
                        }
                    }

                    if (couponEntity.getSelected()){
                        setCouponStatus(false);
                    }
                    //恢复店铺价格
                    reSetStorePrice();
                    List<CouponEntity> selectCoupon = getSelectCoupon();
                    int selectCount = selectCoupon ==null ? 0 : selectCoupon.size();
                    //减去店铺额度
                    reCountPrice(selectCoupon);
                    //检测单张优惠券是否可用
                    couponStatus(getUnSelectCoupon(),selectCount);
                    adapter.notifyDataSetChanged();
                }
                if (isBestCoupon()){
                    setBestCouponStatus(true);
                }else {
                    setBestCouponStatus(false);
                }
                if (isEmptyCoupon()){
                    setCouponStatus(true);
                }else {
                    setCouponStatus(false);
                }
            }
        });
        if (isBestCoupon()){
            setBestCouponStatus(true);
        }else {
            setBestCouponStatus(false);
        }
        if (isEmptyCoupon()){
            setCouponStatus(true);
        }else {
            setCouponStatus(false);
        }
        Log.e("coupon", "list:"+list);
        Log.e("coupon", "22222*********================================");
        //恢复店铺价格
        reSetStorePrice();
        List<CouponEntity> selectCoupon = getSelectCoupon();
        int selectCount = selectCoupon ==null ? 0 : selectCoupon.size();
        //减去店铺额度
        reCountPrice(selectCoupon);
        //检测单张优惠券是否可用
        couponStatus(getUnSelectCoupon(),selectCount);

        Log.e("coupon", "33333*********================================");
    }

    public List<CouponEntity> getUnSelectCoupon() {
        List<CouponEntity> couponEntities = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            CouponEntity couponEntity = list.get(i);
            if (!couponEntity.getSelected()) {
                couponEntities.add(couponEntity);
            }

        }
        return couponEntities;
    }

    public List<CouponEntity> getSelectCoupon() {
        List<CouponEntity> couponEntities = new ArrayList<>();
        if (listSelect != null){
            for (int i = 0; i < listSelect.size(); i++) {
                CouponEntity couponEntity = listSelect.get(i);
                for (int j = 0; j < list.size(); j++) {
                    CouponEntity ce = list.get(j);
                    if (couponEntity.getId().equals(ce.getId())) {
                        couponEntities.add(ce);
                        Log.d("select", "select:" + couponEntity.getId());
                    }

                }
            }
            listSelect.clear();
            listSelect.addAll(couponEntities);
            return listSelect;
        }


        return couponEntities;
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
//        outState.putString("data", data);
//        outState.putString("best", best);
//        outState.putString("select", select);
//        outState.putString("storePrice", storePrice);

        DataManager.getInstance().setCoupons(list);
        DataManager.getInstance().setCouponsBest(listBest);
        DataManager.getInstance().setCouponsSelect(listSelect);

        DataManager.getInstance().storePriceStr = storePrice;
        outState.putBoolean("isSelect", isSelect);
        outState.putBoolean("isBestSelect", isBestSelect);
        outState.putInt("maxCount", maxCount);
        super.onSaveInstanceState(outState);
    }

    void initView() {
        setContentView(R.layout.activity_cart_coupon);
        ivSelector = (ImageView) findViewById(R.id.iv_selector);
        ivBestSelector = (ImageView) findViewById(R.id.iv_best_selector);
        mListView = (LoadMoreListView) findViewById(R.id.listview);
        View footView = View.inflate(this, R.layout.include_footer_coupon, null);//footView
        mListView.addFooterView(footView, null, false);

        tvHistory = findViewById(R.id.tv_history);
        tvHistory.setOnClickListener(this);
        findViewById(R.id.tv_about).setOnClickListener(this);
        basetitle.setText("使用优惠券");
        vException = findViewById(R.id.v_exception);
        mainView = findViewById(R.id.fragment_ptr_home_ptr_frame);
        ivSelector.setOnClickListener(this);
        ivBestSelector.setOnClickListener(this);
        setCouponStatus(isSelect);
        initException();
    }


    public void initException() {
        tvExceptionDescription = (TextView) vException.findViewById(R.id.tv_exception);
        ivExceptionIcon = (ImageView) vException.findViewById(R.id.iv_exception);
        pb = (ProgressBar) vException.findViewById(R.id.pb);
    }


    public void showMainView() {
        vException.setVisibility(View.GONE);
        mainView.setVisibility(View.VISIBLE);
    }


    public void showExceptionView(int imgSrc, String text, int exceptionType) {
        if (imgSrc > 0) {

            findViewById(R.id.rl_cancel).setVisibility(View.GONE);
            findViewById(R.id.rl_best).setVisibility(View.GONE);
        }
        pb.setVisibility(View.GONE);
        vException.setVisibility(View.VISIBLE);
        tvExceptionDescription.setVisibility(View.VISIBLE);
        ivExceptionIcon.setVisibility(View.VISIBLE);
        mainView.setVisibility(View.GONE);

        ivExceptionIcon.setImageResource(imgSrc);
        tvExceptionDescription.setText(text);
        tvExceptionDescription.setTag(exceptionType);
        ivExceptionIcon.setTag(exceptionType);
        tvExceptionDescription.setOnClickListener(this);
        ivExceptionIcon.setOnClickListener(this);
    }

    public void showProgressView() {

        pb.setVisibility(View.VISIBLE);
        vException.setVisibility(View.VISIBLE);
        tvExceptionDescription.setVisibility(View.GONE);
        ivExceptionIcon.setVisibility(View.GONE);
        mainView.setVisibility(View.GONE);
    }


    public void setCouponStatus(boolean isSelect) {
        this.isSelect = isSelect;
        if (isSelect) {
            ivSelector.setImageResource(R.mipmap.select_round);
        } else {
            ivSelector.setImageResource(R.mipmap.unselect_round);
        }
    }

    public void setBestCouponStatus(boolean isSelect) {
        this.isBestSelect = isSelect;
        if (isSelect) {
            ivBestSelector.setImageResource(R.mipmap.select_round);
        } else {
            ivBestSelector.setImageResource(R.mipmap.unselect_round);
        }
    }

    private PtrMdFrameLayout ptrFrameLayout;
    private PtrHandler ptrHandler = new PtrHandler() {
        @Override
        public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
            return false;
        }

        @Override
        public void onRefreshBegin(PtrFrameLayout frame) {
            refresh();
        }
    };

    private void initHeadView() {
        ptrFrameLayout = (PtrMdFrameLayout) findViewById(R.id.fragment_ptr_home_ptr_frame);
        ptrFrameLayout.setPtrHandler(ptrHandler);
        hideProgressDialog();
        showMainView();
    }

    boolean refreshFlag = false;

    public void refresh() {
        mListView.resetNoMore();
        refreshFlag = true;
        //requestOrderList(0);
    }

    @Override
    public void onLoadMore() {
        int number = tagPager.getNumber();
        // requestOrderList( number + 1);
    }


    public void updateViewStatus(RequestTag requestTag) {
        ptrFrameLayout.refreshComplete();
        if (tagPager.isLast()) {
            mListView.noMore();
        } else {
            mListView.stopLoadMore();
        }
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.baseback:
                // 返回上一页
                finish();
                break;
            case R.id.tv_history:
                Intent coupons = new Intent(this, MyHistoryActivity.class);
                startActivity(coupons);
                break;
            case R.id.tv_about:
                Intent pro = new Intent(this, WebActivity.class);
                pro.putExtra(Constant.KEY_WEB_URL, Constant.URL_HELP_COUPON);
                pro.putExtra(Constant.KEY_WEB_TITLE, "优惠券说明");
                startActivity(pro);
                break;
            case R.id.iv_selector:
                setCouponStatus(true);
                setBestCouponStatus(false);
                if (isSelect) {
                    listSelect.clear();
                    clearCouponStatus();
                    //恢复店铺价格
                    reSetStorePrice();
                    List<CouponEntity> selectCoupon = getSelectCoupon();
                    int selectCount = selectCoupon ==null ? 0 : selectCoupon.size();
                    //减去店铺额度
                    reCountPrice(selectCoupon);
                    //检测单张优惠券是否可用
                    couponStatus(getUnSelectCoupon(),selectCount);
                }
                break;
            case R.id.iv_best_selector:
                if (listBest != null && list.size() > 0){
                    listSelect.clear();
                    for (int i=0;i<list.size();i++){
                        CouponEntity couponEntity = list.get(i);
                        CouponEntity best = listBest.get(i);
                        couponEntity.setSelected(best.getSelected());

                        if (best.getSelected()){
                            listSelect.add(couponEntity);
                        }
                    }
                }
                setBestCouponStatus(true);
                setCouponStatus(false);
                //恢复店铺价格
                reSetStorePrice();
                List<CouponEntity> selectCoupon = getSelectCoupon();
                int selectCount = selectCoupon ==null ? 0 : selectCoupon.size();
                //减去店铺额度
                reCountPrice(selectCoupon);
                //检测单张优惠券是否可用
                couponStatus(getUnSelectCoupon(),selectCount);
                adapter.notifyDataSetChanged();
                break;
        }
    }

    public boolean isBestCoupon(){
        if (list != null && listBest != null && list.size() == listBest.size()){
            for (int i=0;i<list.size();i++){
                if(list.get(i).getSelected() != listBest.get(i).getSelected()){
                    return false;
                }
            }
        }else {
            return false;
        }
        return true;
    }
    public boolean isEmptyCoupon(){
        if (list != null ){
            for (int i=0;i<list.size();i++){
                if(list.get(i).getSelected()){
                    return false;
                }
            }
        }else {
            return false;
        }
        return true;
    }


    OnRequestListener<List<CouponEntity>> onRequestListener = new OnRequestListener<List<CouponEntity>>() {
        @Override
        public List<CouponEntity> jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, new TypeToken<List<CouponEntity>>() {
            }.getType());
        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {
            RequestTag requestTag = (RequestTag) tag;
            updateViewStatus(requestTag);
            if (requestSucceedCount == 0) {
                showExceptionView(R.mipmap.order_no, "网络问题,请点击重试", HttpRequest.FAIL_NET_FAULT);
            }
        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {
            if (requestSucceedCount == 0) {
                showExceptionView(R.mipmap.order_no, arg + "", HttpRequest.FAIL_DATA_FAULT);//arg + ",请点击重试"
            }
            RequestTag requestTag = (RequestTag) tag;
            updateViewStatus(requestTag);
        }

        @Override
        public void onStart(Object tag) {
            if (requestSucceedCount == 0) {
                showProgressView();
            }
        }

        @Override
        public void onResponse(ResponseEntity<List<CouponEntity>> response, Object tag) {
            showMainView();
            List<CouponEntity> orderResponse = response.getResponseElement();
            list.addAll(orderResponse);
            adapter.notifyDataSetChanged();
            updateViewStatus(requestTag);
            if (list.size() == 0) {
                showExceptionView(R.mipmap.coupon_no, "还没有优惠券", HttpRequest.FAIL_DATA_EMPTY);
            }

        }
    };


    private RequestTag requestTag = new RequestTag();

    public void requestOrderList(int page) {

        RequestEntityPurchase<Map<String, String>> entity = new RequestEntityPurchase<>();
        Map<String, String> map = new HashMap<>();
        map.put("isValid", "true");
        entity.setCmd(com.epfresh.api.constant.AppPurchaseRouterConstant.cmd_coupon_coupon_list);
        entity.setParameters(map);
        requestTag.tag = com.epfresh.api.constant.AppPurchaseRouterConstant.cmd_coupon_coupon_list;
        requestTag.action = page;
        request(entity, requestTag, onRequestListener);
    }


    class MinusAdapter extends CommonAdapter<CouponEntity> {
        Typeface type;

        public MinusAdapter(Context context, ArrayList<CouponEntity> runPacks, int a) {
            super(context, runPacks, a);
            type = Typeface.createFromAsset(mContext.getAssets(), "coupons.ttf");
        }

        @Override
        public void convert(final ViewHolder holder, final CouponEntity bean) {
            ((TextView) holder.getView(R.id.tv_money)).setTypeface(type);
            holder.setOnClickListener(R.id.tv_rule, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (holder.getView(R.id.tv_rule_detail).getVisibility() == View.GONE) {
                        holder.getView(R.id.tv_rule_detail).setVisibility(View.VISIBLE);
                    } else {
                        holder.getView(R.id.tv_rule_detail).setVisibility(View.GONE);
                    }
                }
            });

            holder.setText(R.id.tv_money, bean.getMinusPrice());
            holder.setText(R.id.tv_hint_main, "满" + bean.getFullPrice() + "元可用");

            holder.setText(R.id.tv_title, bean.getName());

            if (bean.getStoreName() == null || bean.getStoreName().equals("")) {
                holder.getView(R.id.tv_hint_second).setVisibility(View.GONE);
            } else {
                holder.getView(R.id.tv_hint_second).setVisibility(View.VISIBLE);
                holder.setText(R.id.tv_hint_second, "仅限" + bean.getStoreName() + "店铺可用");
            }
            holder.setText(R.id.tv_time, bean.getBeginTime() + "-" + bean.getEndTime());
            holder.setText(R.id.tv_rule_detail, bean.getDescription());

            if (bean.isCanUse()) {
                ((ImageView) holder.getView(R.id.iv_top)).setImageResource(R.mipmap.margin_top_green);
                ((TextView) holder.getView(R.id.tv_money)).setTextColor(getResources().getColor(R.color.green));
                ((TextView) holder.getView(R.id.tv_flag)).setTextColor(getResources().getColor(R.color.green));
                ((ImageView) holder.getView(R.id.iv_flag)).setVisibility(View.VISIBLE);

            } else {
                ((ImageView) holder.getView(R.id.iv_top)).setImageResource(R.mipmap.margin_top_grey);
                ((TextView) holder.getView(R.id.tv_money)).setTextColor(getResources().getColor(R.color.TextColordarklest));
                ((TextView) holder.getView(R.id.tv_flag)).setTextColor(getResources().getColor(R.color.TextColordarklest));
                ((ImageView) holder.getView(R.id.iv_flag)).setVisibility(View.VISIBLE);
            }
            if (bean.getSelected()) {
                ((ImageView) holder.getView(R.id.iv_flag)).setVisibility(View.VISIBLE);
            } else {
                ((ImageView) holder.getView(R.id.iv_flag)).setVisibility(View.GONE);
            }
        }
    }


    public void reCountPrice(List<CouponEntity> couponEntities) {
        Log.e("coupon", "*********================================");
        for (int i = 0; i < couponEntities.size(); i++) {
            CouponEntity couponEntity = couponEntities.get(i);
            Log.d("coupon", "name:" + couponEntity.getId());
            boolean b = reCountPrice(couponEntity, true);
            couponEntity.setCanUse(b);
            Log.d("coupon", "CanUse:" + b);
        }
        Log.e("coupon", "*********---------------------------------");
    }

    public void couponStatus(List<CouponEntity> couponEntities,int selectCount) {
        Log.e("coupon", "================================");
        for (int i = 0; i < couponEntities.size(); i++) {
            CouponEntity couponEntity = couponEntities.get(i);
            Log.d("coupon", "name:" + couponEntity.getId());
            if (selectCount < maxCount ) {
                boolean b = reCountPrice(couponEntity, false);
                Log.d("coupon", "CanUse:" + b);

                couponEntity.setCanUse(b);
            }else {
                couponEntity.setCanUse(false);

            }
        }
        Log.e("coupon", "---------------------------------");
    }

    //重新计算优惠价格
    public void reSetStorePrice() {
        for (Map.Entry<String, StoreGoodsPrice> entry : mapStorePrice.entrySet()) {
            StoreGoodsPrice value = entry.getValue();
            value.reSetDiscountedPrice();
        }

    }

    public void clearCouponStatus() {
        for (int i = 0; i < list.size(); i++) {
            CouponEntity couponEntity = list.get(i);
            couponEntity.setSelected(false);
        }

        adapter.notifyDataSetChanged();
    }


    //计算店铺满减后的店铺中商品的价格,return false,不符合满减;return true,符合满减,boolean isReCount,是否计算,false,用于校验单张优惠券是否可用
    public boolean reCountPrice(CouponEntity couponEntity, boolean isReCount) {
        if (couponEntity == null) {
            return false;
        }
        String type = couponEntity.getType();
        String storeId = couponEntity.getStoreId();
        if ("1".equals(type)) {//平台优惠券
            return averagePrice(couponEntity, isReCount);
        } else if ("2".equals(type) && storeId != null && !"".equals(storeId)) {//店铺优惠券
            //满额
            float fullPrice = couponEntity.getFullPriceFloat();
            //减额
            float minusPrice = couponEntity.getMinusPriceFloat();

            Log.v("coupon", "店铺优惠:fullPrice:"+fullPrice);
            Log.v("coupon", "店铺优惠:minusPrice:"+minusPrice);

            StoreGoodsPrice storeGoodsPrice = mapStorePrice.get(storeId);
            if (storeGoodsPrice != null && fullPrice >= minusPrice) {
//                float discountedPrice = storeGoodsPrice.getDiscountedPrice();
                //满足满减
                if ((int) (storeGoodsPrice.getDiscountedPrice() * 100) >= ((int) (fullPrice * 100))) {
                    if (isReCount)
                        storeGoodsPrice.minusDiscountedPrice(fullPrice);
                    return true;
                }
            }
        }
        return false;
    }


    public boolean averagePrice(CouponEntity couponEntity, boolean isReCount) {
        //满额
        float fullPrice = couponEntity.getFullPriceFloat();
        //减额
        float minusPrice = couponEntity.getMinusPriceFloat();

        Map<String, Double> map = new HashMap<>();

        float total = 0;
        //记录所有店铺价格
        for (Map.Entry<String, StoreGoodsPrice> entry : mapStorePrice.entrySet()) {
            String key = entry.getKey();
            StoreGoodsPrice value = entry.getValue();
            map.put(key, value.getDiscountedPrice());
            //计算优惠券剩余总额度
            total += value.getDiscountedPrice();
            Log.v("coupon", "开始店铺:"+key+"--剩余额度store-discountedPrice:" + value.getDiscountedPrice());
        }

        Log.v("coupon", "总共剩余total:" + total + "---fullPrice:" + fullPrice);
        //满足满减
        if ((int) (total * 100) < ((int) (fullPrice * 100))) {

            return false;
        }

        for (Map.Entry<String, Double> entry : map.entrySet()) {

            String key = entry.getKey();
            //店铺对应的价格
            Double value = entry.getValue();

            //店铺分摊优惠券的价格
            Double v = value / total * fullPrice;

            if (isReCount) {
                StoreGoodsPrice storeGoodsPrice = mapStorePrice.get(key);
                storeGoodsPrice.minusDiscountedPrice(v);
                Log.v("coupon", "结束店铺:"+key+"--剩余额度:" + storeGoodsPrice.getDiscountedPrice() + "---fullPrice:" + fullPrice);
            }
        }

        return true;

    }
}
